//
//  RewardVideoVC.m
//  CJModileAdDemo
//
//  Created by JUN on 2022/3/6.
//

#import "RewardVideoVC.h"
#import "CustomShowView.h"
#import <CJMobileAd/CJMobileAd.h>
#import <Masonry/Masonry.h>
#import <AnyThinkRewardedVideo/AnyThinkRewardedVideo.h>

@interface RewardVideoVC ()<ATRewardedVideoDelegate>

@end

@implementation RewardVideoVC

@synthesize customView = _customView;

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}

- (void)dealloc
{
    NSLog(@"事件:%s", __FUNCTION__);
}
 
- (void)loadAdData {
    [[ATAdManager sharedManager] loadADWithPlacementID:@"b65eadc5e68229" extra:@{} delegate:self];
}

- (void)setupUI {
    self.title = @"RewardVideoAd";
    [self.view addSubview:self.customView];
    self.customView.frame = CGRectMake(0, 100, 200, 200);
    self.customView.center = self.view.center;
}

- (void)handleEvent:(NSInteger)eve {
    if (eve == 0) {
        [self loadAdData];
        return;
    }
    if ([[ATAdManager sharedManager] rewardedVideoReadyForPlacementID:@"b65eadc5e68229"]) {
        [[ATAdManager sharedManager] showRewardedVideoWithPlacementID:@"b65eadc5e68229" inViewController:self delegate:self];
    }
}

- (CustomShowView *)customView {
    if (!_customView) {
        _customView = [[CustomShowView alloc] init];
        __weak RewardVideoVC *weakSelf = self;
        _customView.event = ^(NSInteger eve) {
            [weakSelf handleEvent:eve];
        };
    }
    return _customView;
}

#pragma mark CJRewardVideoAdDelegate
/// Callback when the successful loading of the ad
/// 广告位加载成功
- (void)didFinishLoadingADWithPlacementID:(NSString *)placementID {
    [self.customView canShowStatus:true errorMsg:@""];
    NSLog(@"事件:%s", __FUNCTION__);
}

/// Callback of ad loading failure
/// 广告位加载失败
- (void)didFailToLoadADWithPlacementID:(NSString*)placementID
                                 error:(NSError*)error {
    [self.customView canShowStatus:false errorMsg:@""];
    NSLog(@"事件:%s%@", __FUNCTION__, error);
}
                                 
/// Rewarded video ad play starts
/// 激励视频开始播放(展示回调)
- (void)rewardedVideoDidStartPlayingForPlacementID:(NSString *)placementID
                                             extra:(NSDictionary *)extra {
    NSLog(@"事件:%s", __FUNCTION__);
}

/// Rewarded video ad play ends
/// 激励视频播放完毕
- (void)rewardedVideoDidEndPlayingForPlacementID:(NSString *)placementID
                                           extra:(NSDictionary *)extra {
    NSLog(@"事件:%s", __FUNCTION__);
}

/// Rewarded video ad clicks
/// 激励视频广告被点击
- (void)rewardedVideoDidClickForPlacementID:(NSString *)placementID
                                      extra:(NSDictionary *)extra {
    NSLog(@"事件:%s", __FUNCTION__);
}

/// Rewarded video ad closed
/// 激励视频广告被关闭
/// rewarded:YES:奖励下发，NO:奖励条件未达成
- (void)rewardedVideoDidCloseForPlacementID:(NSString *)placementID
                                   rewarded:(BOOL)rewarded
                                      extra:(NSDictionary *)extra {
    [self.customView canShowStatus:false errorMsg:@""];
    NSLog(@"事件:%s", __FUNCTION__);
}

/// Rewarded video ad reward distribution
/// 激励视频广告奖励下发
- (void)rewardedVideoDidRewardSuccessForPlacemenID:(NSString *)placementID
                                             extra:(NSDictionary *)extra {
    NSLog(@"事件:%s", __FUNCTION__);
}

@end
